package com.yc.UI;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;


import java.util.List;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.dao.OrderDao;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;
import com.yc.utils.Values;

import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.events.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

/**
 *订单面板
 * @author UNO
 * 2021年6月24日下午7:16:38
 */
public class OrderUI extends Composite {
	private Table table;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public OrderUI(Composite parent, int style) {
		super(parent, style);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));
		setLayout(new FillLayout(SWT.HORIZONTAL));

		SashForm sashForm = new SashForm(this, SWT.NONE);
		sashForm.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));
		sashForm.setOrientation(SWT.VERTICAL);

		Composite composite = new Composite(sashForm, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));

		Label label = new Label(composite, SWT.CENTER);
		label.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		label.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));
		label.setBounds(278, 10, 169, 39);
		label.setText("全部订单");

		Composite composite_1 = new Composite(sashForm, SWT.NONE);
		composite_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));
		composite_1.setLayout(new FillLayout(SWT.HORIZONTAL));

		table = new Table(composite_1, SWT.BORDER | SWT.FULL_SELECTION);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		TableColumn tableColumn = new TableColumn(table, SWT.CENTER);
		tableColumn.setWidth(80);
		tableColumn.setText("订单编号");

		TableColumn tableColumn_1 = new TableColumn(table, SWT.CENTER);
		tableColumn_1.setWidth(100);
		tableColumn_1.setText("列车名字");

		TableColumn tableColumn_2 = new TableColumn(table, SWT.CENTER);
		tableColumn_2.setText("乘车人");
		tableColumn_2.setWidth(100);

		TableColumn tableColumn_3 = new TableColumn(table, SWT.CENTER);
		tableColumn_3.setWidth(100);
		tableColumn_3.setText("座位");

		TableColumn tableColumn_4 = new TableColumn(table, SWT.CENTER);
		tableColumn_4.setWidth(150);
		tableColumn_4.setText("发车时间");

		TableColumn tableColumn_5 = new TableColumn(table, SWT.CENTER);
		tableColumn_5.setText("到达时间");
		tableColumn_5.setWidth(150);

		TableColumn tableColumn_6 = new TableColumn(table, SWT.CENTER);
		tableColumn_6.setWidth(100);
		tableColumn_6.setText("座位类型");

		TableColumn tableColumn_7 = new TableColumn(table, SWT.CENTER);
		tableColumn_7.setWidth(70);
		tableColumn_7.setText("票价");

		TableCursor tableCursor = new TableCursor(table, SWT.NONE);
		tableCursor.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));

		Menu menu = new Menu(tableCursor);
		tableCursor.setMenu(menu);

		MenuItem menuItem = new MenuItem(menu, SWT.NONE);
		menuItem.setText("退票");

		MenuItem menuItem_1 = new MenuItem(menu, SWT.NONE);
		menuItem_1.setText("删除订单");
		sashForm.setWeights(new int[] {59, 408});

		//菜单栏点击事件
		menu.addMenuListener(new MenuAdapter() {
			@Override
			public void menuShown(MenuEvent e) {
				TableItem ti = tableCursor.getRow();//获取用户右击选中的行
				String tt = ti.getText(4); //获取第四列对应的String类型的值
				int s = StringUtil.getTimes().compareTo(tt);
				if(s <= 0) {
					menuItem.setEnabled(true);
					menuItem_1.setEnabled(false);
				}
				if(s > 0){
					menuItem.setEnabled(false);
					menuItem_1.setEnabled(true);
				}
			}

		});
		menuItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				TableItem ti = tableCursor.getRow();//获取用户右击选中的行
				String tid = ti.getText(0);//获取第一列的值，即订单编号
				String cname = ti.getText(1);
				String seattype = ti.getText(6);
				String price = ti.getText(7);
				String account = ti.getText(2);
				OrderDao orderdao = new OrderDao();
				int result = orderdao.delete( tid);
				if(result > 0) {
					SWTUtil.showMessage(getShell(), "温馨提示", "退票成功");
					ShowAllOrder();
				}
				
				//票数加一  且 余额加price
				if(seattype.equals("硬座")) {		
					orderdao.updateNumberYZ(1, cname);
					orderdao.updatebalance(Integer.parseInt(price), account);
				}
				if(seattype.equals("软卧")) {
					orderdao.updateNumberRW(1, cname);
					orderdao.updatebalance(Integer.parseInt(price), account);
				}
				if(seattype.equals("硬卧")) {
					orderdao.updateNumberYW(1, cname);
					orderdao.updatebalance(Integer.parseInt(price), account);
				}
				if(seattype.equals("无座")) {
					orderdao.updateNumberWZ(1, cname);
					orderdao.updatebalance(Integer.parseInt(price), account);
				}
			}
		});
		menuItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem ti = tableCursor.getRow();//获取用户右击选中的行
				String tid = ti.getText(0);//获取第一列的值，即订单编号
				OrderDao orderdao = new OrderDao();
				int result = orderdao.delete( tid);
				if(result > 0) {
					ShowAllOrder();
				}
			}
		});
		ShowAllOrder();
	}
	protected void ShowAllOrder()  {
		TableItem ti = null;
		String	account = Values.currentLoginUser.get("u_name").toString();
		OrderDao orderdao = new OrderDao();
		List<Map<String, String>> list = orderdao.findsAllOrder(account);//获取订单信息
		//删除表中原有信息
		table.removeAll();
		if(list == null || list.isEmpty()) {
			return;
		}
		//在表里展示所有信息
		for(Map<String, String> map : list) {
			int s = StringUtil.getTimes().compareTo(map.get("o_starttime"));
			if(s <= 0) {
				ti = new TableItem(table, SWT.NONE);
				ti.setText(new String[]  {map.get("o_id"), map.get("c_name"), map.get("o_name"), map.get("o_seat"), map.get("o_starttime"), map.get("o_endtime"), map.get("o_zwstatus"), map.get("o_price")});
			}
			if(s > 0) {
				ti = new TableItem(table, SWT.NONE);
				ti.setForeground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
				ti.setText(new String[]  {map.get("o_id"), map.get("c_name"), map.get("o_name"), map.get("o_seat"), map.get("o_starttime"), map.get("o_endtime"), map.get("o_zwstatus"), map.get("o_price")});
			}

		}
	}
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
